﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace Mengoo.BDMTier
{
    /// <summary>
    /// 实体与关系数据库之间的转换
    /// </summary>
    public static class ModelHelper
    {
        /// <summary>
        /// 将DataTable里的第一条记录转换为T类型
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="dtInfo">要转换的DataTable</param>
        /// <returns>类型T</returns>
        public static T DataTable2T<T>(DataTable dtInfo)
        {
            if (dtInfo == null || dtInfo.Rows.Count == 0)
            {
                return default(T);
            }

            T model = (T)Activator.CreateInstance(typeof(T));

            PropertyInfo[] pi = model.GetType().GetProperties();
            foreach (PropertyInfo p in pi)
            {
                for (int m = 0; m < dtInfo.Columns.Count; m++)
                {
                    if (p.Name.ToLower().Equals(dtInfo.Columns[m].ColumnName.ToLower()))
                    {
                        if (dtInfo.Rows[0][m] != DBNull.Value)
                        {
                            p.SetValue(model, dtInfo.Rows[0][m], null);
                        }
                        else
                        {
                            p.SetValue(model, null, null);
                        }

                        break;
                    }
                }
            }

            return model;
        }

        /// <summary>
        /// 将DataTable里的记录转换为存储类型T的List
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="dtInfo">要转换的DataTable</param>
        /// <returns>存储类型T的List</returns>
        public static List<T> DataTable2List<T>(DataTable dtInfo)
        {
            if (dtInfo == null || dtInfo.Rows.Count == 0)
            {
                return null;
            }

            List<T> list = new List<T>();

            for (int i = 0; i < dtInfo.Rows.Count; i++)
            {
                T model = (T)Activator.CreateInstance(typeof(T));

                PropertyInfo[] pi = model.GetType().GetProperties();

                foreach (PropertyInfo p in pi)
                {
                    for (int m = 0; m < dtInfo.Columns.Count; m++)
                    {
                        // 属性与字段名称一致的进行赋值 
                        if (p.Name.Equals(dtInfo.Columns[m].ColumnName))
                        {
                            // 数据库NULL值单独处理 
                            if (dtInfo.Rows[i][m] != DBNull.Value)
                            {
                                p.SetValue(model, dtInfo.Rows[i][m], null);
                            }
                            else
                            {
                                p.SetValue(model, null, null);
                            }

                            break;
                        }
                    }
                }

                list.Add(model);
            }

            return list;
        }

        public static bool IsType(Type type, string typeName)
        {
            if (type.ToString() == typeName)
            {
                return true;
            }
            return false;
        }
    }
}
